//
//  HYRecommended.m
//  HYHoney
//
//  Created by dllo on 16/1/19.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "HYRecommended.h"
#import "NetworkHandeler.h"
#import "SecondOfModel.h"
#import "SecondOfCell.h"
#import "PushOfURL.h"
#import "DKNightVersion.h"

@interface HYRecommended ()<NetworkHandelerDelegate, UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, retain) UICollectionView *collection;
@property (nonatomic, retain) NSMutableArray *arr;

@end

@implementation HYRecommended

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"更多精选";
    
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:1 green:1 blue:1 alpha:1], NSForegroundColorAttributeName, nil]];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"xial.png"] style:UIBarButtonItemStylePlain target:self action:@selector(presentLeftMenuViewController:)];
    
    [self handelData];
    [self createCollection];
}


- (void)dealloc{
    
    [_arr release];
    [_collection release];
    [_flowLayout release];
    [super dealloc];
}



- (void)handelData{
    
    NSString *str = @"https://api-v2.wanyicool.com/wanyi/api/v2/timestamp/1453464290499/number/1/topic/publish";
    
    [NetworkHandeler handelerJSONWithURL:str delegate:self];
}


- (void)handelerDidComplete:(id)result{

    self.arr = [NSMutableArray array];
    
    for (NSDictionary *dic in result) {
        
        SecondOfModel *model = [[SecondOfModel alloc] init];
        
        model.title = [dic objectForKey:@"title"];
        model.imageUrl = [dic objectForKey:@"imageUrl"];
        model.briefDesc = [dic objectForKey:@"briefDesc"];
        model.shareUrl = [dic objectForKey:@"shareUrl"];
        
        [self.arr addObject:model];
    }
    
    [self.collection reloadData];
}


- (void)createFlowLayout{

    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.itemSize = CGSizeMake((self.view.frame.size.width - 50) / 2, self.view.frame.size.width - 65);
    
    self.flowLayout.minimumLineSpacing = 30;
    self.flowLayout.minimumInteritemSpacing = 20;
    self.flowLayout.sectionInset = UIEdgeInsetsMake(30, 15, 15, 15);
}


- (void)createCollection{

    [self createFlowLayout];
    
    self.collection = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:self.flowLayout];
    [self.view addSubview:self.collection];
    
    self.collection.dataSource = self;
    self.collection.delegate = self;
    
    [self.collection registerClass:[SecondOfCell class] forCellWithReuseIdentifier:@"pool1"];
    self.collection.showsVerticalScrollIndicator = NO;
    
    self.collection.dk_backgroundColorPicker = DKColorWithRGB(0xFFFAFA, 0x27408B);
    
    
    [self.collection release];
}


// 实现协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return self.arr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    SecondOfCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool1" forIndexPath:indexPath];
    
    SecondOfModel *model = [self.arr objectAtIndex:indexPath.item];
    [cell passmodel:model];
    
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    PushOfURL *pushVC = [[PushOfURL alloc] init];
    
    SecondOfModel *model = [self.arr objectAtIndex:indexPath.row];
    [pushVC passSecondModel:model];
    
    [self.navigationController pushViewController:pushVC animated:NO];
    
    [pushVC release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
